package com.atguigu.guli.service.trade.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.trade.entity.Order;
import com.atguigu.guli.service.trade.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-13
 */

@RestController
@RequestMapping("api/trade/order")
public class ApiOrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/auth/isBuy/{courseId}")
    public R isBuy(@PathVariable String courseId,HttpServletRequest request){
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getCourseId,courseId);
        queryWrapper.eq(Order::getMemberId,JwtHelper.getId(request));
        queryWrapper.eq(Order::getStatus,"1");
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("isBuy",order==null?"0":"1");
    }


    @PostMapping("/auth/createOrder/{courseId}")
    public R createOrder(HttpServletRequest request, @PathVariable String courseId){
        String memberId = JwtHelper.getId(request);
        String orderId = orderService.createOrder(memberId, courseId);
        return R.ok().data("item",orderId);

    }

    @GetMapping("/auth/getOrder/{orderId}")
    public R getOrderById(HttpServletRequest request,@PathVariable String orderId){
        String memberId = JwtHelper.getId(request);
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getId,orderId);
        queryWrapper.eq(Order::getMemberId,memberId);
        Order order = orderService.getOne(queryWrapper);
        return R.ok().data("item",order);
    }
}

